package com.my.mymafengwo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smallstrong.mafengwopro.R;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.my.mymafengwo.adapter.BaseAdapterHelper;
import com.my.mymafengwo.adapter.QuickAdapter;
import com.my.mymafengwo.base.BaseActivity;
import com.my.mymafengwo.bean.CommonSquares;
import com.my.mymafengwo.bean.EliteList;
import com.my.mymafengwo.bean.SalesMdds;
import com.my.mymafengwo.utils.CircleTransformUtils;
import com.my.mymafengwo.utils.UrlUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * ***********************************************
 * author:ljz
 * time:2015/12/5 13:42
 * name:往期蜂首，正在热卖，目的地推荐 的显示页面
 * step:
 * ***********************************************
 */
public class MoreResultActivity extends BaseActivity {
    private ImageView ivBack;
    private GridView gv;
    private TextView tvTitle;
    private String str;//从首页传来的数据
    private boolean isBottom = false;//记录gridView是否滑动到最底部
    private int num = 0;//表示要进行第几次上拉加载
    private QuickAdapter<CommonSquares.DataEntity.ListEntity> adapterCommonSquares;
    private QuickAdapter<SalesMdds.DataEntity.ListEntity> adapterSalesmdds;
    private QuickAdapter<EliteList.DataEntity.ListEntity> adapterOldElite;
    /**
     * CommonSquares集合
     */
    private List<CommonSquares.DataEntity.ListEntity> listCommonSquares;
    /**
     * SalesMdds集合
     */
    private List<SalesMdds.DataEntity.ListEntity> listSalesMdds;
    /**
     * 往期蜂首的集合
     */
    private List<EliteList.DataEntity.ListEntity> listOldElite = new ArrayList<EliteList.DataEntity.ListEntity>();
    ;

    @Override
    protected void loadXml() {
        setContentView(R.layout.activity_commonsquares);
    }

    @Override
    protected void getIntentData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        str = intent.getStringExtra("str");
    }

    @Override
    protected void initViews() {
        ivBack = (ImageView) findViewById(R.id.iv_commonsquares);
        gv = (GridView) findViewById(R.id.gv_commonsquares);
        tvTitle = (TextView) findViewById(R.id.tv_title);
    }

    @Override
    protected void registerListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        switch (str) {
            case "往期蜂首":
                findMoreElite(UrlUtils.OLD_ELITE);
                break;
            case "正在热卖":
                findMoreHotBuy();
                break;
            case "全年当季推荐":
                findMoreDestination();
                break;
        }
    }

    /**
     * 查看往期蜂首
     */
    private void findMoreElite(String url) {
        tvTitle.setText(str);
        gv.setNumColumns(1);
        LoadEliteData(url);
    }

    public void LoadEliteData(String url) {
        HttpUtils http = new HttpUtils();
        http.configDefaultHttpCacheExpiry(100 * 1024);
        http.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                Gson gson = new Gson();
                EliteList eliteList = gson.fromJson(result, EliteList.class);
                listOldElite.addAll(eliteList.getData().getList());
                OldEliteIn();
            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
    }

    /**
     * 查看更多超值热卖
     */
    private void findMoreHotBuy() {
        tvTitle.setText(str);
        HttpUtils http = new HttpUtils();
        http.configDefaultHttpCacheExpiry(100 * 1024);
        http.send(HttpRequest.HttpMethod.GET, UrlUtils.HOT_BUY_URL, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                Gson gson = new Gson();
                SalesMdds salesMdds = gson.fromJson(result, SalesMdds.class);
                listSalesMdds = salesMdds.getData().getList();
                salesMddsIn();
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    /**
     * 查看更多目的地推荐
     */
    private void findMoreDestination() {
        tvTitle.setText(str);
        HttpUtils http = new HttpUtils();
        http.configDefaultHttpCacheExpiry(100 * 1024);
        http.send(HttpRequest.HttpMethod.GET, UrlUtils.DESTINATION_URL, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                Gson gson = new Gson();
                CommonSquares commonSquares = gson.fromJson(result, CommonSquares.class);
                listCommonSquares = commonSquares.getData().getList();
                commonSquaresIn();
            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
    }

    /**
     * 往期蜂首图片下载及填入
     */
    private void OldEliteIn() {
        adapterOldElite = new QuickAdapter<EliteList.DataEntity.ListEntity>(this, R.layout.list_item_old_elite, listOldElite) {
            @Override
            protected void convert(BaseAdapterHelper helper, EliteList.DataEntity.ListEntity item) {
                helper.setText(R.id.tv_do, item.getTitle());
                helper.setText(R.id.tv_who, item.getUser().getName());
                helper.setText(R.id.tv_where, item.getMdds().get(0).getName());
                helper.setText(R.id.tv_number, item.getNum_visit() + "浏览");
                BitmapUtils bm = new BitmapUtils(MoreResultActivity.this);
                ImageView ivSmall = helper.getView(R.id.iv_small);
                ImageView ivBig = helper.getView(R.id.iv_big);

                Picasso.with(MoreResultActivity.this).load(item.getThumbnail()).into(ivBig);
                Picasso.with(MoreResultActivity.this).load(item.getUser().getLogo()).transform(new CircleTransformUtils()).into(ivSmall);
            }
        };
        gv.setAdapter(adapterOldElite);
        num++;
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                jumpActivity(listOldElite.get(position).getUser().getJump_url());
            }
        });
        gv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    case SCROLL_STATE_IDLE://停止滑动
                        if (isBottom) {
                            switch (num) {
                                case 1://表示第一次上拉加载数据
                                    findMoreElite(UrlUtils.OLD_ELITE_PAGE_1);
                                    Toast.makeText(MoreResultActivity.this, "正在加载", Toast.LENGTH_LONG).show();
                                    break;
                                case 2:
                                    findMoreElite(UrlUtils.OLD_ELITE_PAGE_2);
                                    Toast.makeText(MoreResultActivity.this, "正在加载", Toast.LENGTH_LONG).show();
                                    break;
                                case 3:
                                    findMoreElite(UrlUtils.OLD_ELITE_PAGE_3);
                                    Toast.makeText(MoreResultActivity.this, "正在加载", Toast.LENGTH_LONG).show();
                                    break;
                                case 4:
                                    findMoreElite(UrlUtils.OLD_ELITE_PAGE_4);
                                    Toast.makeText(MoreResultActivity.this, "正在加载", Toast.LENGTH_LONG).show();
                                    break;
                                case 5:
                                    findMoreElite(UrlUtils.OLD_ELITE_PAGE_5);
                                    Toast.makeText(MoreResultActivity.this, "正在加载", Toast.LENGTH_LONG).show();
                                    break;
                                case 6:
                                    findMoreElite(UrlUtils.OLD_ELITE_PAGE_6);
                                    Toast.makeText(MoreResultActivity.this, "正在加载", Toast.LENGTH_LONG).show();
                                    break;
                                case 7:
                                    findMoreElite(UrlUtils.OLD_ELITE_PAGE_7);
                                    Toast.makeText(MoreResultActivity.this, "正在加载", Toast.LENGTH_LONG).show();
                                    break;
                                case 8:
                                    findMoreElite(UrlUtils.OLD_ELITE_PAGE_8);
                                    Toast.makeText(MoreResultActivity.this, "正在加载", Toast.LENGTH_LONG).show();
                                    break;
                                case 9:
                                    findMoreElite(UrlUtils.OLD_ELITE_PAGE_9);
                                    Toast.makeText(MoreResultActivity.this, "正在加载", Toast.LENGTH_LONG).show();
                                    break;
                                case 10:
                                    findMoreElite(UrlUtils.OLD_ELITE_PAGE_10);
                                    Toast.makeText(MoreResultActivity.this, "正在加载", Toast.LENGTH_LONG).show();
                                    break;
                                case 11:
                                    Toast.makeText(MoreResultActivity.this, "数据已加载完成", Toast.LENGTH_LONG).show();
                                    break;
                            }
                        }
                        break;
                    case SCROLL_STATE_TOUCH_SCROLL://手指触摸滑动
                        break;
                    case SCROLL_STATE_FLING://惯性滑动
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem + visibleItemCount == totalItemCount) {
                    isBottom = true;
                } else {
                    isBottom = false;
                }
            }
        });
    }

    /**
     * 正在热卖图片下载及填入
     */
    private void salesMddsIn() {
        adapterSalesmdds = new QuickAdapter<SalesMdds.DataEntity.ListEntity>(this, R.layout.list_item_salesmdds, listSalesMdds) {

            @Override
            protected void convert(BaseAdapterHelper helper, SalesMdds.DataEntity.ListEntity item) {
                helper.setText(R.id.tv_salesmdds_where, item.getTitle());
                helper.setText(R.id.tv_salesmdds_time, item.getSub_title());
                helper.setText(R.id.tv_salesmdds_money, item.getPrice());
                helper.setImageUrl(R.id.iv_salesmdds, item.getThumbnail());
            }
        };
        gv.setAdapter(adapterSalesmdds);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                jumpActivity(listSalesMdds.get(position).getJump_url());
            }
        });
    }

    /**
     * 目的地推荐的图片下载和填入
     */
    private void commonSquaresIn() {
        adapterCommonSquares = new QuickAdapter<CommonSquares.DataEntity.ListEntity>(this, R.layout.list_item_commonsquares, listCommonSquares) {
            @Override
            protected void convert(BaseAdapterHelper helper, CommonSquares.DataEntity.ListEntity item) {
                helper.setText(R.id.tv_month, item.getTitle());
                helper.setText(R.id.tv_sub_title, item.getSub_title());
                helper.setImageUrl(R.id.iv_commonsquares, item.getImg_url());
            }
        };
        gv.setAdapter(adapterCommonSquares);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                jumpActivity(listCommonSquares.get(position).getJump_url());
            }
        });
    }

    /**
     * 跳转到 TravelNotesActivity 页面并传递数据
     *
     * @param url
     */
    public void jumpActivity(String url) {
        Intent intent = new Intent(MoreResultActivity.this, TravelNotesActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
    }
}